﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Diagnostics;

public partial class MasterPages_Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PreRender += new EventHandler(InitPage);
        }
    }

    protected void InitPage(object sender, EventArgs e)
    {
        InitCategories();
        InitCategoryDishes();
    }
    protected void OnCategoryDishDataBound(object sender, System.EventArgs e)
    {
        ////GridView gridview = (GridView)sender;
        ////if (!((SqlDataReader)(gridview.DataSource)).IsClosed)
        ////    ((SqlDataReader)(gridview.DataSource)).Close();
    }
    protected void InitCategoryDishes()
    {
        Int16 restaurantId = Convert.ToInt16(Session["restaurant_id"].ToString());

        Entities.Restaurant restaurant = BLL.Command.GetRestaurant(restaurantId);

        categorydish_gridview.DataBound += new EventHandler(OnCategoryDishDataBound);
        categorydish_gridview.DataSource = restaurant.Menu.AllDishesDataReader;
        categorydish_gridview.DataBind();

        // Initialize category for eachrow
        foreach (GridViewRow row in categorydish_gridview.Rows)
        {
            DropDownList categoryDropdownlist = (DropDownList)row.Cells[3].FindControl("category_dropdownlist");
            Int16 categoryId = Convert.ToInt16(((Label)row.Cells[3].FindControl("category_id_label")).Text);
            categoryDropdownlist.DataSource = restaurant.Menu.Categories;
            categoryDropdownlist.DataBind();
            categoryDropdownlist.SelectedValue = categoryId.ToString();
            categoryDropdownlist.Enabled = false;
        }
    }
    protected void InitCategories()
    {
        Int16 restaurantId = Convert.ToInt16(Session["restaurant_id"].ToString());

        Entities.Restaurant restaurant = BLL.Command.GetRestaurant(restaurantId);

        category_gridview.DataSource = restaurant.Menu.Categories;
        category_gridview.DataBind();
    }

    protected void CategoryDishRowUpdatingHandler(object sender, GridViewUpdateEventArgs e)
    {
        GridView theGridView = (GridView)sender;
        GridViewRow row = theGridView.Rows[e.RowIndex];

        Int16 categoryDishId = Convert.ToInt16(theGridView.DataKeys[e.RowIndex].Value.ToString());
        float price = (float)Convert.ToDouble(((TextBox)(row.Cells[1].Controls[0])).Text.Trim());
        Boolean available = ((CheckBox)(row.Cells[2].Controls[0])).Checked;
        Int16 categoryId = Convert.ToInt16(((DropDownList)(row.Cells[3].FindControl("category_dropdownlist"))).SelectedValue);

        Entities.CagetoryDish categoryDish = new Entities.CagetoryDish();
        categoryDish.Id = categoryDishId;
        categoryDish.Price = price;
        categoryDish.Available = available;
        categoryDish.CategoryId = categoryId;

        BLL.Command.UpdateCategoryDish(ref categoryDish);

        theGridView.EditIndex = -1;
        InitCategoryDishes();
    }
    protected void CategoryRowUpdatingHandler(object sender, GridViewUpdateEventArgs e)
    {
        GridView theGridView = (GridView)sender;
        GridViewRow row = theGridView.Rows[e.RowIndex];

        Int16 id = Convert.ToInt16(theGridView.DataKeys[e.RowIndex].Value.ToString());
        String name = ((TextBox)(row.Cells[0].Controls[0])).Text.Trim();
        String description = ((TextBox)(row.Cells[1].Controls[0])).Text.Trim();

        Entities.Category category = new Entities.Category(id);
        category.Name = name;
        category.Description = description;

        BLL.Command.UpdateCategory(ref category);

        theGridView.EditIndex = -1;
        InitCategories();
        InitCategoryDishes();
    }
    protected void RowDeleteHandler(object sender, GridViewDeleteEventArgs e)
    {
        String restaurantDishId = categorydish_gridview.DataKeys[e.RowIndex].Values[0].ToString();

        //BLL.Command.DelCategoryDish(restaurantDishId);

        categorydish_gridview.EditIndex = -1;

        InitCategoryDishes();
    }


    protected void RowEditingHandler(object sender, GridViewEditEventArgs e)
    {
        GridView gridview = (GridView)sender;
        if (gridview.ID=="category_gridview")
        {
            category_gridview.EditIndex = e.NewEditIndex;
            InitCategories();
        }
        else if (gridview.ID == "categorydish_gridview")
        {
            categorydish_gridview.EditIndex = e.NewEditIndex;
            InitCategoryDishes();

            ((DropDownList)(categorydish_gridview.Rows[e.NewEditIndex].Cells[3].FindControl("category_dropdownlist"))).Enabled = true;
        }
    }
    protected void EditCancelingHandler(object sender, GridViewCancelEditEventArgs e)
    {
        categorydish_gridview.EditIndex = -1;

        InitCategoryDishes();
    }

    
    
}
